import { defineStore } from 'pinia'
import { sessionCache } from '@/utils/cache'
import { loginApi } from '@/service/api/login/login'
import { ElNotification } from 'element-plus'

interface userState {
  loading: Boolean
  userInfo: any
}

const useUserStore = defineStore('user', {
  state: (): userState => ({
    loading: false,
    userInfo: {}
  }),

  actions: {
    onLoading(loading: Boolean) {
      this.loading = loading
    },

    logout() {
      this.userInfo = {}
      sessionCache.setCache('userInfo', '')
      sessionCache.setCache('permissionList', '')
      sessionCache.setCache('sliderBarIndex', '')
      sessionCache.removeCache('firstLogin')

      location.reload()
    },

    // 登录
    async loginSystem(form: any) {
      const { data } = await loginApi(form)
      sessionCache.setCache('userInfo', data)
      this.userInfo = data
      console.log(data)
      ElNotification({ title: '提示', message: '登录成功', type: 'success' })
    }
  }
})

export default useUserStore
